//
//  KSActionSheet.m
//  AWIconSheet
//
//  Created by Narcissus on 10/26/12.
//  Copyright (c) 2012 Narcissus. All rights reserved.
//

#import "SGActionSheet.h"
#import <QuartzCore/QuartzCore.h>

#define CANCEL_BTN_HEIGHT   40

#define CELL_ROW_HEIGHT     100

@interface SGActionSheet()<UIScrollViewDelegate> {
//    UIView          *_bgView;
    UIScrollView    *_scrollView;
}

@property (nonatomic, strong) NSMutableArray* items;

@end

@implementation SGActionSheet

- (id)initWithFrame:(CGRect)frame rowCount:(NSInteger)rowCount {
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        self.userInteractionEnabled = YES;
        
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_backgroundView setBackgroundColor:[UIColor blackColor]];
        _backgroundView.alpha = 0.3;
        [self addSubview:_backgroundView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [self addGestureRecognizer:tapGesture];
    }
    
    NSInteger scroll_view_height = CELL_ROW_HEIGHT * rowCount;
    [self initScrollViewWithHeight:scroll_view_height];
    [self initCancelBtn];
    return self;
}

- (void)setContentWithImageArray:(NSArray *)imgArray titleArray:(NSArray *)titleArray {
    const NSInteger nRowCount = [imgArray count] / 3 + ([imgArray count] % 3 > 0 ? 1 : 0);
    const CGFloat SCROLL_VIEW_WIDHT = _scrollView.bounds.size.width;
    for (NSInteger rowIndex = 0; rowIndex < nRowCount; rowIndex ++) {
        CGRect frame = CGRectMake(0, CELL_ROW_HEIGHT * rowIndex, SCROLL_VIEW_WIDHT / 3, CELL_ROW_HEIGHT);
        NSInteger arrayIndex = rowIndex * 3;
        SGActionSheetCell *cell1 = [[SGActionSheetCell alloc] initWithFrame:frame];
        [cell1.titleLabel setText:[titleArray objectAtIndex:arrayIndex]];
        [cell1.iconBtnView setImage:[UIImage imageNamed:[imgArray objectAtIndex:arrayIndex]] forState:UIControlStateNormal];
        [cell1.iconBtnView addTarget:self action:@selector(iconBtnViewClick:) forControlEvents:UIControlEventTouchUpInside];
        cell1.iconBtnView.tag = arrayIndex;
        [_scrollView addSubview:cell1];
        
        if (++arrayIndex >= [titleArray count]) {
            break;
        }
        
        frame.origin.x += SCROLL_VIEW_WIDHT / 3;
        SGActionSheetCell *cell2 = [[SGActionSheetCell alloc] initWithFrame:frame];
        [cell2.titleLabel setText:[titleArray objectAtIndex:arrayIndex]];
        [cell2.iconBtnView setImage:[UIImage imageNamed:[imgArray objectAtIndex:arrayIndex]] forState:UIControlStateNormal];
        [cell2.iconBtnView addTarget:self action:@selector(iconBtnViewClick:) forControlEvents:UIControlEventTouchUpInside];
        cell2.iconBtnView.tag = arrayIndex;
        [_scrollView addSubview:cell2];
        
        if (++arrayIndex >= [titleArray count]) {
            break;
        }
        
        frame.origin.x += SCROLL_VIEW_WIDHT / 3;
        SGActionSheetCell *cell3 = [[SGActionSheetCell alloc] initWithFrame:frame];
        [cell3.titleLabel setText:[titleArray objectAtIndex:arrayIndex]];
        [cell3.iconBtnView setImage:[UIImage imageNamed:[imgArray objectAtIndex:arrayIndex]] forState:UIControlStateNormal];
        [cell3.iconBtnView addTarget:self action:@selector(iconBtnViewClick:) forControlEvents:UIControlEventTouchUpInside];
        cell3.iconBtnView.tag = arrayIndex;
        [_scrollView addSubview:cell3];
    }
}

- (void)initScrollViewWithHeight:(NSInteger)height {
    CGFloat startY = self.bounds.size.height - height - 2 * 10 - CANCEL_BTN_HEIGHT;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, startY, SCREEN_WIDTH - 2*10, height)];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setBackgroundColor:[UIColor whiteColor]];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setShowsVerticalScrollIndicator:NO];
    [_scrollView setBounces:NO];
    [_scrollView setScrollEnabled:YES];
    _scrollView.layer.masksToBounds = YES;
    _scrollView.layer.cornerRadius = 5;
    _scrollView.userInteractionEnabled = YES;
    
    [self addSubview:_scrollView];
}

- (void)initCancelBtn {
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:COLOR_WITH_RGB(0x0069ff) forState:UIControlStateNormal];
    [cancelBtn setBackgroundColor:[UIColor whiteColor]];
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 5;
    [cancelBtn setFrame:CGRectMake(10, self.bounds.size.height - CANCEL_BTN_HEIGHT - 10, self.bounds.size.width - 2 * 10, CANCEL_BTN_HEIGHT)];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:cancelBtn];
}

- (void)cancelBtnClick:(id)sender {
    [self removeView];
}

- (void)iconBtnViewClick:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectItem:index:)]) {
        [_delegate didSelectItem:self index:btn.tag];
        [self removeView];
    }
}


- (void)removeView
{
    self.backgroundView.alpha = 0.f;
    [UIView animateWithDuration:0.5f animations:^{
        self.top = self.height;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showInView:(UIView *)theView {
    [theView addSubview:self];
    
    self.top = SCREEN_HEIGHT;
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.5f animations:^{
        self.top = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2f animations:^{
            self.backgroundView.alpha = 0.3;
        }];
    }];
}

@end

#pragma mark - SGActionSheetCell

@implementation SGActionSheetCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        const CGFloat startX = (self.bounds.size.width - 57) / 2;
        const CGFloat startY = (self.bounds.size.height - 57 - 20) / 2;
        _iconBtnView = [UIButton buttonWithType:UIButtonTypeCustom];
        [_iconBtnView setFrame:CGRectMake(startX, startY, 57, 57)];
        [_iconBtnView setBackgroundColor:[UIColor clearColor]];
        [[_iconBtnView layer] setCornerRadius:8.0f];
        [[_iconBtnView layer] setMasksToBounds:YES];
        [_iconBtnView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:_iconBtnView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(startX - 5, startY + 57 + 5, 57 + 10, 14)];
        [_titleLabel setBackgroundColor:[UIColor clearColor]];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        [_titleLabel setTextColor:COLOR_WITH_RGB(0x484848)];
//        [_titleLabel setShadowColor:[UIColor blackColor]];
//        [_titleLabel setShadowOffset:CGSizeMake(0, 0.5)];
        [_titleLabel setText:@""];
        [self addSubview:_titleLabel];
    }
    return self;
}

@end


